<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="index1 in list.length">
					<!--有订单展示时候-->
					<!-- 888 {{index1-1}}  {{orderList[index1-1].length}} {{hasData}} -->
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box" v-if="hasData">
							<view class="order" v-for="(order, index) in orderList[index1-1]" :key="order.id">
								<!--订单区-->
								<view class="top" @click="gotoOrderDetail(order.id)">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{order.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<!-- <view class="right">{{order.deal}} order.deal</view> -->
									<!-- <view class="right">{{order.orderDetails}}</view> -->
									<view class="right">￥{{ order.amount }}</view>
								</view>
								<!--商品区-->
								<view class="item" v-for="(goods, index) in order.orderDetails.data" :key="index">
									<view class="left">
										<image :src="goods.goods.cover_url" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ goods.goods.title }}</view>
										<view class="delivery-time">发货时间 2022-05-25 00:00:00 </view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(goods.goods.price) }}
											<text class="decimal">.{{ priceDecimal(goods.goods.price) }}</text>
										</view>
										<view class="number">x{{ goods.num }}</view>
										
										<view v-if="order.status===4" class="evaluate btn" @click="jump(`/pages/comment/comment`,goods.goods.id,order.id)">评价</view>
									</view>
								</view>
								<!--统计区-->
								<view class="total">
									共{{ totalNum(order.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										￥{{order.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more">
										<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
									</view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<!-- <view class="evaluate btn" @click="jump(`/pages/comment/comment`,goods.goods_id)">评价</view> -->
									<view v-if="order.status===1 || order.status===2" class="evaluate btn" @click="fahuo(order.id)">发货</view>
									<view v-if="order.status===3" class="evaluate btn" @click="qianshou(order.id)">签收</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
						<!--无订单展示区域-->
						<view class="page-box" v-else>
							<view>
								<view class="centre">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="goto">随便逛逛</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [
					[],
					[],
					[],
					[],
					[],
					[]
				],
				dataList: [],
				list: [{
						name: '全部'
					},
					{
						name: '待支付'
					},
					{
						name: '已支付'
					},
					{
						name: '已发货'
					},
					{
						name: '已收货',
						count: 12
					},
					{
						name: '过期',
						count: 15
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore', 'loadmore', 'loadmore'],
				page: 1,
				title: '',
				status: 0,
				total_pages: 0, //总页数
				hasData: false
			};
		},
		async onLoad() {
			this.getOrderList();
		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			async reachBottom() {
				console.log(this.page, this.total_pages)
				if (this.page >= this.total_pages) {
					console.log('已经是最后一页了')
					return false
				}
				// 此tab为空数据
				/* if(this.current != 2) {
					this.loadStatus.splice(this.current,1,"loading")
					setTimeout(() => {
						this.getOrderList(this.current);
					}, 1200);
				} */
				this.page++
				this.getOrderList()
			},
			// 页面数据
			async getOrderList() {
				const ordersRes = await this.$u.api.ordersList(this.page, this.title, this.status)
				console.log('ordersRes', ordersRes)
				this.dataList = ordersRes.data
				this.orderList[this.swiperCurrent] = this.orderList[this.swiperCurrent].concat(this.dataList)
				//console.log(this.swiperCurrent,this.orderList[0])
				this.total_pages = ordersRes.meta.pagination.total_pages

				this.hasData = this.orderList[this.swiperCurrent].length > 0 ? true : false

				this.loadStatus.splice(this.current, 1, "loadmore")
			},
			// 总价
			totalPrice(item) {
				//无商品的情况
				if (!item) {
					return 0
				}
				let price = 0;
				item.map(val => {
					price += parseFloat(val.price);
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				//无商品的情况
				if (!item) {
					return 0
				}
				//console.log('item',item)
				let num = 0;
				item.map(val => {
					num += val.num;
				});
				return num;
			},
			// tab栏切换
			async change(index) {
				this.swiperCurrent = index;
				this.page = 1
				this.total_pages = 0
				console.log('change', index)
				for (let k = 0; k < this.orderList.length; k++) {
					this.orderList[k] = []
				}
				this.status = this.swiperCurrent
				//this.getOrderList(index);
				this.getOrderList()
			},
			transition({
				detail: {
					dx
				}
			}) {
				//console.log('transition')
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				console.log('animationfinish')
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;

				this.page = 1
				this.total_pages = 0
				console.log('animationfinish current', current)
				for (let k = 0; k < this.orderList.length; k++) {
					this.orderList[k] = []
				}
				this.status = this.swiperCurrent
				//this.getOrderList(index);
				this.getOrderList()
			},
			goto() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			//订单详情
			gotoOrderDetail(orderId){
				uni.reLaunch({
					url: '/pages/order/order_detail?orderId='+orderId
				})
			},
			// 商品详情跳转
			jump(url, goodsId,orderId) {
				console.log('jump', url)
				this.$u.route({
					url,
					params: {
						goodsId,
						orderId
					}
				})
			},
			async fahuo(orderId){
				const params={}
				params.express_type='YTO'
				params.express_no='78272298050551'
				const postRes = await this.$u.api.ordersPost(orderId,params)
				console.log('post',postRes)
				this.getOrderList();
			},
			//签收
			async qianshou(orderId){
				const postRes = await this.$u.api.ordersConfirm(orderId)
				console.log('qianshou',res)
				this.getOrderList();
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;
	
	.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
				.btn {
					line-height: 52rpx;
					width: 160rpx;
					border-radius: 26rpx;
					border: 2rpx solid $u-border-color;
					font-size: 26rpx;
					text-align: center;
					color: $u-type-info-dark;
				}
				
				.evaluate {
					color: $u-type-warning-dark;
					border-color: $u-type-warning-dark;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
